<template>
    <div class="mainDiv" style="padding:20px;box-sizing: border-box;">
        <el-row :gutter="20" style="margin: 0px">
            <el-col :span="12" class="mb20">
                <el-input v-model="goods.name" class="mt mb20" placeholder="请输入产品名称！">
                    <template slot="prepend">产品名称</template>
                </el-input>
                <el-input v-model="goods.number" placeholder="请输入产品编号！" class="mt mb20">
                    <template slot="prepend">产品编号</template>
                </el-input>
                <el-input v-model="goods.price" class="mt mb20" placeholder="请输入产品价格！">
                    <template slot="prepend">标准价格</template>
                </el-input>
                <el-input v-model="goods.nums" placeholder="请输入商品库存！" class="mt mb20">
                    <template slot="prepend">商品库存</template>
                </el-input>
                <el-input type="textarea" v-model="goods.remarks" class="mt" placeholder="请输入产品描述！"></el-input>
            </el-col>
            <el-col :span="12" class="mb20">
                <el-input v-model="cname" class="mt mb20" placeholder="请选择分类" disabled>
                    <template slot="prepend">所属分类</template>
                    <el-button slot="append" icon="el-icon-search" @click="selcate"></el-button>
                </el-input>
                <el-input v-model="bname" placeholder="请选择品牌" disabled>
                    <template slot="prepend">所属品牌</template>
                    <el-button slot="append" icon="el-icon-search" @click="selbran"></el-button>
                </el-input>
                销售提成
                <el-radio-group v-model="goods.tdtype" class="mt mb20" style="margin-left:10px;">
                    <el-radio :label="1">百分比提成</el-radio>
                    <el-radio :label="2">固定金额提成</el-radio>
                </el-radio-group>
                <el-input v-model="goods.percent" placeholder="销售百分比提点（0 ~ 100）！" v-if="goods.tdtype==1">
                    <template slot="prepend">百分比提点</template>
                </el-input>
                <el-input v-model="goods.fixed" placeholder="销售固定金额提成！" v-else>
                    <template slot="prepend">固定金额提成</template>
                </el-input>
                卡耗提成
                <el-radio-group v-model="goods.khtdtype" class="mt mb20" style="margin-left:10px;">
                    <el-radio :label="1">百分比提成</el-radio>
                    <el-radio :label="2">固定金额提成</el-radio>
                </el-radio-group>
                <el-input v-model="goods.khpercent" placeholder="卡耗百分比提点（0 ~ 100）！" v-if="goods.khtdtype==1">
                    <template slot="prepend">百分比提点</template>
                </el-input>
                <el-input v-model="goods.khfixed" placeholder="卡耗固定金额提成！" v-else>
                    <template slot="prepend">固定金额提成</template>
                </el-input>
            </el-col>
        </el-row>
        <el-row style="padding:20px;">
            <el-col :span="12">
                <el-button size="mini" @click.native="close" style="width:90%;display:block;">取消</el-button>
            </el-col>
            <el-col :span="12">
                <el-button size="mini" type="success" @click.native="callSubmit" style="width:90%;display:block;" :loading="loading">确定</el-button>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
export default {
    name: 'userselectzhiwei',
    data() {
        return {
            goods: {
                id: 0,
                name: '',
                price: '',
                categoryid: 0,
                brandid: 0,
                remarks: '',
                nums: 0,
                percent: '',
                fixed: '',
                tdtype: 1,
                khpercent: '',
                khfixed: '',
                khtdtype: 1,
                number: '',
            },
            bname: '',
            cname: '',
            loading: false
        }
    },
    mounted() {
        let RequestUrl = C.getDatas()
        if (RequestUrl.row) {
            let obj = RequestUrl.row
            this.goods.id = obj.id
            this.goods.name = obj.name
            this.goods.price = obj.price
            this.goods.percent = Number(obj.percent * 100).toFixed(0)
            this.goods.fixed = obj.fixed
            this.goods.tdtype = obj.tdtype
            this.goods.khpercent = Number(obj.khpercent * 100).toFixed(0)
            this.goods.khfixed = obj.khfixed
            this.goods.khtdtype = obj.khtdtype
            this.goods.categoryid = obj.categoryid
            this.goods.brandid = obj.brandid
            this.goods.remarks = obj.remarks
            this.goods.nums = obj.nums
            this.goods.number = obj.number
            this.bname = obj.bname
            this.cname = obj.cname
        }
    },
    methods: {
        selcate: async function() {
            // let res = await C.openBrowserWindow('/html/organization/popup/selectcategory.html', {}, { width: 600, height: 800 })
            let res = await C.openBrowserWindow('/selectPopupCategory', {}, {
                transparent: true,
                frame: false,
                width: 600,
                height: 800,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                this.goods.categoryid = res.id
                this.cname = res.name
            }
        },
        selbran: async function() {
            // let res = await C.openBrowserWindow('/html/organization/popup/selectbrand.html', {}, { width: 1000, height: 800 })
            let res = await C.openBrowserWindow('/selectPopupBrand', {}, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 800,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                this.goods.brandid = res.id
                this.bname = res.name
            }
        },
        async callSubmit(index) {
            if (/^\s*$/.test(this.goods.name)) {
                C.error('产品名称不能为空！')
                return false
            } else if (this.goods.number === '') {
                C.error('产品编号不能为空！')
                return false
            } else if (!/^[1-9]+\d*(\.)?\d*$/.test(this.goods.price)) {
                C.error('请输入产品价格！')
                return false
            }
            if (this.goods.tdtype == 1) {
                if (!/^(\d|[1-9]\d|100)$/.test(this.goods.percent)) {
                    C.error('请输入产品销售提点（0 ~ 100）')
                    return false
                }
            } else {
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.goods.fixed)) {
                    C.error('请输入产品销售固定金额提成')
                    return false
                }
            }
            if (this.goods.khtdtype == 1) {
                if (!/^(\d|[1-9]\d|100)$/.test(this.goods.khpercent)) {
                    C.error('请输入产品卡耗提点（0 ~ 100）')
                    return false
                }
            } else {
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.goods.khfixed)) {
                    C.error('请输入产品卡耗固定金额提成')
                    return false
                }
            }
            if (this.goods.categoryid == 0) {
                C.error('请选择产品所属分类！')
                return false
            } else if (this.goods.brandid == 0) {
                C.error('请选择产品所属品牌！')
                return false
            }
            let ssum = 0
            let khsum = 0
            if (this.goods.tdtype === 1) {
                ssum = parseInt(this.goods.price) * parseInt(this.goods.percent) / 100
            } else {
                ssum = parseInt(this.goods.fixed)
            }
            if (this.goods.khtdtype === 1) {
                khsum = parseInt(this.goods.price) * parseInt(this.goods.khpercent) / 100
            } else {
                khsum = parseInt(this.goods.khfixed)
            }
            let allsum = parseInt(ssum + khsum)

            if (this.goods.price < allsum) {
                C.error('输入的提成超出项目价格')
                return false
            }
            let that = this
            that.loading = true
            let data = await C.ajax('/organization/goods/addedit', this.goods, 'post')
            if (data) {
                if (data.errno === 0) {
                    that.loading = false
                    that.$notify({
                        title: '成功',
                        message: data.data,
                        type: 'success'
                    })
                    C.closeBrowserWindow(data)
                } else {
                    that.loading = false
                    that.$notify.error({
                        title: '错误',
                        message: data.errmsg
                    })
                }
            }
        },
        close() {
            C.closeBrowserWindow()
        }
    }
}
</script>
<style>
.mb20 {
    margin-bottom: 20px;
}
</style>